package edu.hawaii.myisern.brown.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.meterware.httpunit.TableCell;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
 * Tests operation of the MyIsern researcher web pages.
 * 
 * @author Randy Cox
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class TestOrganizationActionBeans {

  /** Get the test host. */
  private String testHost = (System.getProperty("test.host") == null) ? "http://localhost:8080/"
      : System.getProperty("test.host");

  /** User name to use. */
  private String username = "test";

  /** Password name to use. */
  private String password = "test";

  /**
   * Test the main menu new organization button.
   * 
   * @throws Exception If problem occurs.
   */
  @Test
  public void testMainMenuNewOrganizationButton() throws Exception {
    WebConversation conversation = new WebConversation();

    // Get login page and check title
    String Url = testHost + "myisern-brown";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Checking index.jsp retrieval", "MyIsern - Login",
        response.getTitle());

    // Push login button
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", this.username);
    request.setParameter("password", this.password);
    WebResponse mainPage = response = conversation.getResponse(form
        .getRequest());
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Main Menu", response.getTitle());

    // Click on new researcher link.
    WebLink link = response.getLinkWithID("NewOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result",
        "MyIsern - Edit Organization", response.getTitle());

    // Click on cancel.
    form = response.getFormWithID("EditOrganizationForm");
    response = conversation.getResponse(form.getRequest("cancel"));
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Main Menu", response.getTitle());

    // Click on new researcher link.
    link = response.getLinkWithID("NewOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking button result",
        "MyIsern - Edit Organization", response.getTitle());

    // Click on save.
    form = response.getFormWithID("EditOrganizationForm");
    form.setParameter("name", "Test Organization");
    form.setParameter("contact", "Test Contact");
    form.setParameter("homePage", "Test Home Page");
    form.setParameter("country", "Test Country");
    form.setParameter("researchDescription",
        "Test Researcher Description");
    form.setParameter("newAffiliatedResearcher", "Test1");
    form.setParameter("newResearchKeyword", "Word1");
    form.setParameter("type", "Test Type");
    response = conversation.getResponse(form.getRequest("save"));
    assertEquals("Checking mainMenu.jsp retrieval",
        "MyIsern - Edit Organization", response.getTitle());

    // Click on edit researcher link, brining up select list.
    response = mainPage;
    link = response.getLinkWithID("EditOrganizationLink");
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking result", "MyIsern - Select Organization",
        response.getTitle());

    // Check if new name is in select list and click it.
    WebTable table = response.getTableWithID("SelectOrganizationTable");
    TableCell cell = table.getTableCellWithID("Test Organization");
    assertNotNull("Check if cell exisits", cell);
    link = cell.getLinkWith("Test");
    assertNotNull("Check if cell exisits", link);

    // Check that we are back at Edit screen, and check the fields for
    // data.
    response = conversation.getResponse(link.getRequest());
    assertEquals("Checking result", "MyIsern - Edit Organization",
        response.getTitle());
    form = response.getFormWithID("EditOrganizationForm");
    assertEquals("Check field.", "Test Organization", form
        .getParameterValue("name"));
    assertEquals("Check field.", "Test Contact", form
        .getParameterValue("contact"));
    assertEquals("Check field.", "Test Home Page", form
        .getParameterValue("homePage"));
    assertEquals("Check field.", "Test Country", form
        .getParameterValue("country"));
    assertEquals("Check field.", "Test Researcher Description", form
        .getParameterValue("researchDescription"));
    assertEquals("Check field.", "Test Type", form
        .getParameterValue("type"));
  }
}
